#include "mac_whitelist.h"
#include "ui_mac_whitelist.h"

MacWhiteList::MacWhiteList(QWidget *parent) :
    BaseEditor(parent),
    ui(new Ui::MacWhiteList)
{
    ui->setupUi(this);

    table=TD_MAC_ADDR;

    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);

    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);

    updateDepartment();
    createMenu();
}

MacWhiteList::~MacWhiteList()
{
    delete ui;
}

void MacWhiteList::keyPressEvent(QKeyEvent *event)
{
    if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S)
        save();
    else if(event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_Z)
        revert();
    else{
        switch (event->key()) {
        case Qt::Key_F1:
            newItem();
            break;
        case Qt::Key_F4:
            updateDepartment();
            break;
        case Qt::Key_F5:
            updateView();
            break;
        case Qt::Key_Delete:
            removeItem();
            break;
        }
    }
}

void MacWhiteList::generateMenu()
{
    menu->addAction(QStringLiteral("新记录... (F1)"),this,&MacWhiteList::newItem);
    menu->addSeparator();
    menu->addAction(QStringLiteral("更新列表 (F4)"),this,&MacWhiteList::updateDepartment);
    menu->addAction(QStringLiteral("更新视图 (F5)"),this,&MacWhiteList::updateView);
    auto optMenu=menu->addMenu("选项");
    optMenu->addAction("删除",this,&MacWhiteList::removeItem);
    optMenu->addSeparator();
    optMenu->addAction("撤销",this,&MacWhiteList::revert);
    optMenu->addAction("保存",this,&MacWhiteList::save);
}

void MacWhiteList::updateDepartment()
{
    if(listModel->rowCount() != 0)
        listModel->setStringList(QStringList());

    auto items=sp_getCategory("department",table);
    if(!items.isEmpty())
        listModel->setStringList(items);
}

void MacWhiteList::newItem()
{
    Mac_AddItem item(this);
    if(item.exec() == QDialog::Accepted)
        updateView();
}

void MacWhiteList::removeItem()
{
    auto index=ui->tableView->currentIndex();
    if(index.isValid())
        model->removeRow(index.row());
}

void MacWhiteList::updateView()
{
    model->setTable(table);
    model->setFilter(QString("department='%1'").arg(selectDepartment));
    model->select();
    setHeaderData();
}

void MacWhiteList::setHeaderData()
{
    ui->tableView->hideColumn(0);
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("部门"));
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("姓名"));
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("设备类型"));
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("物理地址"));
}

void MacWhiteList::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        selectDepartment=index.data().toString();
        if(!selectDepartment.isEmpty())
            updateView();
    }
}

void MacWhiteList::on_kw_editingFinished()
{
    QString keyword=ui->kw->text().trimmed();
    if(!keyword.isEmpty())
    {
        if(keyword.contains(":"))
            keyword=keyword.replace(":","-");

        model->setTable(table);
        model->setFilter(QString("name like '%%1%' OR mac like '%%2%'").arg(keyword).arg(keyword));
        model->select();
        setHeaderData();
    }
}
